﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;

namespace ktskumar.SharePoint.List
{
    public partial class MoreSettings : LayoutsPageBase
    {
        private SPList m_List;

        private string m_ListURL;

        protected SPList List
        {
            get
            {
                if (this.m_List == null)
                {
                    this.m_List = base.Web.Lists.GetList(new Guid(base.Request.QueryString.GetValues("List")[0]), true);
                }
                return this.m_List;
            }
        }

        protected string ListURL
        {
            get
            {
                if (this.m_ListURL == null)
                {
                    SPFolder rootfolder =  base.Web.Lists.GetList(new Guid(base.Request.QueryString.GetValues("List")[0]), true).RootFolder;
                    this.m_ListURL =base.Site.Url+ rootfolder.ServerRelativeUrl.Replace(rootfolder.Name, "");
                }
                return this.m_ListURL;
            }
        }


     

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            ThrowIfNoListQueryString();          

            base.PageTarget = this.List;
            if (this.Page.IsPostBack)
            {
                return;
            }

            this.EnableListYes.Checked = List.Hidden;
            this.EnableListNo.Checked = !EnableListYes.Checked;
                                 

            txtListURL.Text = List.RootFolder.Name;

            if (List.Hidden)
                txtListURL.Enabled = false;
            else
                txtListURL.Enabled = true;
        }

        private void ThrowIfNoListQueryString()
        {
            string[] values = HttpContext.Current.Request.QueryString.GetValues("List");
            if ((values == null) || (values.Length == 0))
            {
                throw new SPException(SPResource.GetString("InvalidQueryString", new object[] { "List" }));
            }

        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string redirecturl = "";

                if (List.Hidden != EnableListYes.Checked)
                {
                    List.Hidden = EnableListYes.Checked;
                    List.Update();
                    List.ParentWeb.Update();
                }

                if (List.Hidden)
                    redirecturl = List.ParentWebUrl;
                else
                    redirecturl = List.DefaultViewUrl;
                

                
                if (List.RootFolder.Name.ToUpper() != txtListURL.Text.ToUpper())
                {
                    
                    SPFolder folder = List.RootFolder;
                    string url = folder.ServerRelativeUrl.Replace(folder.Name, txtListURL.Text);
                    folder.MoveTo(url);

                    if(!EnableListYes.Checked)
                    redirecturl = folder.ServerRelativeUrl.ToString();
                }
                Response.Redirect(redirecturl);
            }
            catch (Exception ex)
            {
                ltmsg.Text = ex.Message;
            }

        }

 




        
    }
}
